import {
  sysml
} from 'src/api/sysml-imports';
import { graphHelper } from 'src/api/GraphHelper';
import { treeForEach, findNodeByValue, getNodePath, isActDiagramElement } from 'src/util/common';
import { RelationShipGenerator } from 'src/api/generator/relationship-create-generator';
import { GenericTableApi } from 'src/api/shape/GenericTableApi';
import { modelApi } from 'src/api/model/modelApi';

export default {
  getAllColumn(id) {
    let diagram = graphHelper.getGraphNodeById(id, app.activeProjectId).model;
    const getAllColumnIds = GenericTableApi.getAllColumnIds({ diagram });
    if (getAllColumnIds.data != null) {
      return getAllColumnIds.data;
    }
  },
  GenerateRelationship(menus, model){
    function mapData(methodName){
      return function(i){
        return { ...i, label: i.name, icon: 'statics/images/sysml/' + i.key + '.svg', method: methodName };
      };
    }
    const OutgoingMenu = findNodeByValue(menus, 'Outgoing');
    OutgoingMenu.children =  RelationShipGenerator.generateOutgoing(model).map(mapData('outgoing'));
    if (OutgoingMenu.children.length === 0){
      OutgoingMenu.disabled = true;
    }

    const IncomingMenu = findNodeByValue(menus, 'Incoming');
    IncomingMenu.children = RelationShipGenerator.generateIncoming(model).map(mapData('incoming'));
    if (IncomingMenu.children.length === 0){
      IncomingMenu.disabled = true;
    }
    return menus;
  },
  setColumnId: function(id, { columnIds }) {
    const diagram = graphHelper.getGraphNodeById(id, app.activeProjectId).model;

    GenericTableApi.setColumnIds({ diagram, columnIds });
  },
  getData: function(id, rootNode) {
    //// const tableData = GenericTableApi.getTableData({ diagram });
    const tableData = {
      visibleRows: rootNode.umlDiagramFrame.children,
      columnWidthes: [],
    };
    return tableData;

  },
  removeFromTable: function(id, { row }) {
    const diagram = graphHelper.getGraphNodeById(id, app.activeProjectId).model;
    
    GenericTableApi.removeRowShape({ diagram, row });
  },
  setScopeByElement: function(id, { rowElements }){
    const diagram = graphHelper.getGraphNodeById(id, app.activeProjectId).model;

    GenericTableApi.setScopeByElement({ diagram, rowElements });
  },
  deleteNode: function(targetModel) {
    const ids = modelApi.deleteModelElement({ model: targetModel });
    app.$bus.emit('model-delete', { ids });
  }

};
